<script>
import { getAction } from '@api/manage'
import '@/assets/less/TableExpand.less'
import { JeecgListMixin } from '@/mixins/JeecgListMixin'

export default {
  name: 'SelectInventoryMaterialModal',
  mixins:[JeecgListMixin],
  props: {
    // 多选/单选，checkbox or radio
    rowSelectionType: {
      type: String,
      default: 'checkbox',
    },
  },
  data() {
    return {
      title: '选择库存物料',
      visible: false,
      disableSubmit: false,
      list: [],
      url: {
        list: '/wms/inventory/list',
      },
      columns: [
        {
          title: '#',
          dataIndex: '',
          key: 'rowIndex',
          width: 30,
          align: 'center',
          customRender: function (t, r, index) {
            return parseInt(index) + 1
          },
        },
        {
          title: '单据类型',
          align: 'center',
          dataIndex: 'entryType_dictText',
          fixed: 'left',
        },
        {
          title: '单据号',
          align: 'center',
          dataIndex: 'documentNumber',
          fixed: 'left',
        },
        {
          title: '物料编码',
          align: 'center',
          dataIndex: 'materialCode',
        },
        {
          title: '物料名称',
          align: 'center',
          dataIndex: 'materialName',
        },
        {
          title: '规格型号',
          align: 'center',
          dataIndex: 'specificationModel',
        },
        // {
        //   title: '物料描述',
        //   align: 'center',
        //   dataIndex: 'materialDesc'
        // },
        {
          title: '单位',
          align: 'center',
          dataIndex: 'unit_dictText',
        },
        {
          title: '数量',
          align: 'center',
          dataIndex: 'quantity',
        },
        {
          title: '辅单位',
          align: 'center',
          dataIndex: 'secondaryUnit_dictText',
        },
        {
          title: '辅数量',
          align: 'center',
          dataIndex: 'secondaryQuantity',
        },
        {
          title: '生产日期',
          align: 'center',
          dataIndex: 'productionDate',
          customRender: function (text) {
            return !text ? '' : text.length > 10 ? text.substr(0, 10) : text
          },
        },
        {
          title: '保质期（天）',
          align: 'center',
          dataIndex: 'shelfLife',
        },
        {
          title: '限用日期',
          align: 'center',
          dataIndex: 'expirationDate',
          customRender: function (text) {
            return !text ? '' : text.length > 10 ? text.substr(0, 10) : text
          },
        },
        {
          title: '批次号',
          align: 'center',
          dataIndex: 'batchNumber',
        },
        {
          title: '物料批号',
          align: 'center',
          dataIndex: 'materialBatchNumber',
        },
        {
          title: '储位',
          align: 'center',
          dataIndex: 'storageLocation_dictText',
        },
        {
          title: '入库日期',
          align: 'center',
          dataIndex: 'entryDate',
          customRender: function (text) {
            return !text ? '' : text.length > 10 ? text.substr(0, 10) : text
          },
        },
        {
          title: '库龄（天）',
          align: 'center',
          dataIndex: 'inventoryAge',
        },
        {
          title: '质检状态',
          align: 'center',
          dataIndex: 'qualityInspectionStatus_dictText',
        },
        {
          title: '供应商名称',
          align: 'center',
          dataIndex: 'supplierName',
        },
        {
          title: '入库单号',
          align: 'center',
          dataIndex: 'entryNumber',
        },
        {
          title: '客户物料编码',
          align: 'center',
          dataIndex: 'customerMaterialCode',
        },
        {
          title: '宝洁订单号',
          align: 'center',
          dataIndex: 'pgOrderNumber',
        },
        {
          title: '订单编号',
          align: 'center',
          dataIndex: 'orderNumber',
        },
        {
          title: '生产车间',
          align: 'center',
          dataIndex: 'productionShop_dictText',
        },
        {
          title: '宝洁采购订单号',
          align: 'center',
          dataIndex: 'pgPurchaseOrderNumber',
        },
        {
          title: '宝洁供应商编号',
          align: 'center',
          dataIndex: 'pgSupplierCode',
        },
        {
          title: '宝洁供应商',
          align: 'center',
          dataIndex: 'pgSupplierName',
        },
        {
          title: '客户名称',
          align: 'center',
          dataIndex: 'customerName',
        },
        {
          title: '物料分类1',
          align: 'center',
          dataIndex: 'materialClassify_dictText',
        },
        {
          title: '物料分类2',
          align: 'center',
          dataIndex: 'brand_dictText',
        },
        {
          title: '更新时间',
          align: 'center',
          dataIndex: 'updateTime',
          customRender: function (text) {
            return !text ? '' : text.length > 10 ? text.substr(0, 10) : text
          },
        },
        {
          title: '制单人',
          align: 'center',
          dataIndex: 'creator_dictText',
        },
        {
          title: '制单人部门',
          align: 'center',
          dataIndex: 'creatorDeptCode_dictText',
        },
        {
          title: '制单日期',
          align: 'center',
          dataIndex: 'creationDate',
          customRender: function (text) {
            return !text ? '' : text.length > 10 ? text.substr(0, 10) : text
          },
        },
        {
          title: '经办人',
          align: 'center',
          dataIndex: 'handler_dictText',
        },
        {
          title: '经办人部门',
          align: 'center',
          dataIndex: 'handlerDeptCode_dictText',
        },
        {
          title: '经办日期',
          align: 'center',
          dataIndex: 'handlingDate',
          customRender: function (text) {
            return !text ? '' : text.length > 10 ? text.substr(0, 10) : text
          },
        },
        {
          title: '检验标准',
          align: 'center',
          dataIndex: 'inspectionStandard',
        },
        {
          title: '抽检数量',
          align: 'center',
          dataIndex: 'samplingQuantity',
        },
        {
          title: '检验结果',
          align: 'center',
          dataIndex: 'inspectionResult',
        },
        {
          title: '检验人',
          align: 'center',
          dataIndex: 'inspector_dictText',
        },
        {
          title: '检验部门',
          align: 'center',
          dataIndex: 'inspectionDeptCode_dictText',
        },
        {
          title: '检验日期',
          align: 'center',
          dataIndex: 'inspectionDate',
          customRender: function (text) {
            return !text ? '' : text.length > 10 ? text.substr(0, 10) : text
          },
        },
        {
          title: '备注',
          align: 'center',
          dataIndex: 'remark',
          scopedSlots: { customRender: 'remark' },
        },
      ],
      ipagination: {
        current: 1,
        pageSize: 10,
        pageSizeOptions: ['10', '20', '30'],
        showTotal: (total, range) => {
          return range[0] + '-' + range[1] + ' 共' + total + '条'
        },
        showQuickJumper: true,
        showSizeChanger: true,
        total: 0,
      },
      loading: false,
      selectedRowKeys: [],
      selectionRows: [],
      queryParam: {
        pageNo: 1,
        pageSize: 10,
      },
      customParam: null,
      materialCode: null,
      warehouseCode: null,
    }
  },
  mounted() {},
  methods: {
    showModal(params) {
      if (params) {
        const { materialCode, warehouseCode, customParam } = params
        this.customParam = customParam
        if (materialCode) {
          this.queryParam.materialCode = materialCode
          this.materialCode = materialCode
        }
        if (warehouseCode) {
          this.queryParam.warehouseCode = warehouseCode
          this.warehouseCode = warehouseCode
        }
      }
      this.fetchData()
      this.visible = true
    },
    fetchData() {
      this.loading = true;
      this.queryParam.qualityInspectionStatus = '1';
      getAction(this.url.list, this.queryParam).then((res) => {
        if (!res.success) {
          this.$message.warning(res.message)
          return
        }
        let result = res.result
        if (Number(result.total) > 0) {
          this.ipagination.total = Number(result.total)
          this.list = res.result.records
        } else {
          this.list = []
        }
      }).finally(() => {
        this.loading = false
      })
    },

    handleConfirm() {
      this.$emit('confirm', this.selectionRows, this.customParam)
      this.close()
    },
    handleCancel() {
      this.close()
    },
    onClearSelected() {
      this.selectedRowKeys = []
      this.selectionRows = []
    },
    onSelectRow(selectedRowKeys, selectionRows) {
      this.selectedRowKeys = selectedRowKeys
      this.selectionRows = selectionRows
    },
    handleTableChange(pagination, filters, sorter) {
      //分页、排序、筛选变化时触发
      if (Object.keys(sorter).length > 0) {
        this.isorter.column = sorter.field
        this.isorter.order = 'ascend' === sorter.order ? 'asc' : 'desc'
      }
      this.ipagination = pagination
      this.queryParam.pageNo = this.ipagination.current
      this.queryParam.pageSize = this.ipagination.pageSize
      this.fetchData()
    },
    close() {
      this.onClearSelected()
      this.$emit('close')
      this.visible = false
    },
    searchQuery(){
      this.fetchData();
    },
    searchReset(){
      this.queryParam= {
        pageNo: 1,
        pageSize: 10,
      }
      if (this.materialCode) {
        this.queryParam.materialCode = this.materialCode
      }
      if (this.warehouseCode) {
        this.queryParam.warehouseCode = this.warehouseCode
      }
      this.fetchData();
    },
  },
}
</script>

<template>
  <j-modal
    :title="title"
    :width="1200"
    :visible="visible"
    :maskClosable="false"
    switchFullscreen
    @ok="handleConfirm"
    :okButtonProps="{ class: { 'jee-hidden': disableSubmit } }"
    @cancel="handleCancel"
  >
    <div style="padding: 10px">
      <div class="table-page-search-wrapper">
        <a-form layout="inline" @keyup.enter.native="searchQuery">
          <a-row :gutter="24">
            <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item label="物料编码">
              <a-input placeholder="请输入物料编码" v-model="queryParam.materialCode"></a-input>
            </a-form-item>
          </a-col>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item label="物料名称">
              <a-input placeholder="请输入物料名称" v-model="queryParam.materialName"></a-input>
            </a-form-item>
          </a-col>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item label="单据号">
              <a-input placeholder="请输入单据号" v-model="queryParam.documentNumber"></a-input>
            </a-form-item>
          </a-col>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item label="批次号">
              <a-input placeholder="请输入批次号" v-model="queryParam.batchNumber"></a-input>
            </a-form-item>
          </a-col>
            <a-col :xl="6" :lg="7" :md="8" :sm="24">
              <span style="float: left; overflow: hidden" class="table-page-search-submitButtons">
                <a-button type="primary" @click="searchQuery" icon="search">查询</a-button>
                <a-button type="primary" @click="searchReset" icon="reload" style="margin-left: 8px">重置</a-button>
              </span>
            </a-col>
          </a-row>
        </a-form>
      </div>

      <div class="ant-alert ant-alert-info" style="margin-bottom: 16px">
        <i class="anticon anticon-info-circle ant-alert-icon"></i> 已选择
        <a style="font-weight: 600">{{ selectedRowKeys.length }}</a
        >项
        <a style="margin-left: 24px" @click="onClearSelected">清空</a>
      </div>
      <a-table
        ref="table"
        size="middle"
        :scroll="{ x: true }"
        bordered
        rowKey="id"
        :columns="columns"
        :dataSource="list"
        :pagination="ipagination"
        :loading="loading"
        :rowSelection="{ selectedRowKeys, onChange: onSelectRow, type: rowSelectionType }"
        class="j-table-force-nowrap1"
        @change="handleTableChange"
      >
        <template slot="remark" slot-scope="text, record">
          <div v-if="record.creatorRemark">制单人:{{ record.creatorRemark }}</div>
          <div v-if="record.handlerRemark">经办人:{{ record.handlerRemark }}</div>
          <div v-if="record.inspectorRemark">检验人:{{ record.inspectorRemark }}</div>
        </template>
      </a-table>
    </div>
  </j-modal>
</template>

<style scoped></style>
